// Copyright 2008 Senary
// See credits.txt for details.

// This file is part of Omega VII.

// Omega VII is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Omega VII is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Omega VII.  If not, see <http://www.gnu.org/licenses/>.

#include <sstream>

#include "Panel.h"

using namespace std;

Panel::Panel(std::pair<int, int> position, int layer)
	: _pPanel(0), _position(position), _layer(layer)
{
	ostringstream definition;
	definition << "pos_x = " << _position.first << ";\n";
	definition << "pos_y = " << _position.second << ";\n";

	_panelDefinition = definition.str();
}

Panel::~Panel(void)
{
	if (_pPanel != 0) {
		pan_remove(_pPanel);
		_pPanel = 0;
	}
}


// gets the size of the panel (for centered positioning)
pair<int, int> Panel::getSize()
{
	return make_pair(_INT(_pPanel->size_x), _INT(_pPanel->size_x));
}


// Sets resp. changes the position of the panel
void Panel::setPosition(std::pair<int, int> position)
{
	if(_pPanel==0) return;

	_position = position;
	if (_pPanel != 0) {
		_pPanel->pos_x = _VAR(position.first);
		_pPanel->pos_y = _VAR(position.second);
	}
}

//create PANEL useable by engine
void Panel::createPanel()
{
	if(_pPanel != 0) {
		pan_remove(_pPanel);
	}
	_pPanel = pan_create(const_cast<char*>(_panelDefinition.c_str()), _VAR(_layer));
	_pPanel->alpha = _VAR(100);
}

// param timestep: time since the last update, in seconds
// param totalTime: total time the game has been running
void Panel::update(float timestep, float totalTime)
{
}

// Makes the panel visible
void Panel::display()
{
	if(_pPanel==0) return;

	_pPanel->flags |= VISIBLE;
}

// Hides the panel
void Panel::hide()
{
	if(_pPanel==0) return;

	_pPanel->flags &= ~VISIBLE;
}


// Sets the opaqueness of the panel
void Panel::setAlpha(int alpha)
{
	_pPanel->alpha = _VAR(alpha);
}



//is this panel visible?
bool Panel::isVisible(void) {
	if(_pPanel==0) return false;
	
	return (_pPanel->flags & VISIBLE)==VISIBLE;
}


//check if the point is within the rectangular body of the panel
bool Panel::withinRange(std::pair<int, int> position)
{
	if(_pPanel==0) return false;
	else if(!(_pPanel->flags & VISIBLE)) return false; //invisible button is unreachable

	if(position.first < _INT(_pPanel->pos_x) ||
			position.first > _INT(_pPanel->pos_x+_pPanel->size_x) ||
			position.second < _INT(_pPanel->pos_y) ||
			position.second > _INT(_pPanel->pos_y+_pPanel->size_y))
		return false;

	return true;
}